package com.woniuxy.joylifeshop.service.serviceImpl;

import com.woniuxy.common.entity.ShopMerchantIncome;
import com.woniuxy.common.entity.ShopMerchantIncomeExample;
import com.woniuxy.common.entity.ShopOrderItem;
import com.woniuxy.common.mapper.ShopMerchantIncomeMapper;
import com.woniuxy.joylifeshop.exception.MerchantIncomeException;
import com.woniuxy.joylifeshop.service.MerchantIncomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class MerchantIncomeServiceImpl implements MerchantIncomeService {

    @Autowired
    ShopMerchantIncomeMapper shopMerchantIncomeMapper;

    /**
    * @Author SooYoo
    * @Description 修改商家的收入信息
    * @Param 
    * @Return 
    **/
    @Override
    public void modifyMerchantIncomeInformationByCondition(ShopOrderItem shopOrderItem) {

        try {
            //生成商家收入对象信息
            ShopMerchantIncome shopMerchantIncome = new ShopMerchantIncome();
            //增加其条目
            shopMerchantIncome.setIncomeId(UUID.randomUUID().toString());
            shopMerchantIncome.setIncomeCount(shopOrderItem.getPrice());
            shopMerchantIncome.setIncomeDate(new Date());
            shopMerchantIncome.setItemId(shopOrderItem.getItemId());
            shopMerchantIncome.setMerchantId(shopOrderItem.getMerchantId());
            //插入项目
            shopMerchantIncomeMapper.insert(shopMerchantIncome);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantIncomeException();
        }


    }
    
    /**
    * @Author SooYoo
    * @Description
    * @Param 
    * @Return 
    **/
    @Override
    public List<ShopMerchantIncome> readMerchantIncome(ShopMerchantIncome shopMerchantIncome) {

        try {
            ShopMerchantIncomeExample shopMerchantIncomeExample = new ShopMerchantIncomeExample();
            ShopMerchantIncomeExample.Criteria criteria = shopMerchantIncomeExample.createCriteria();
            criteria.andMerchantIdEqualTo(shopMerchantIncome.getMerchantId());

            if (shopMerchantIncome.getIncomeId() != null && !shopMerchantIncome.getIncomeId().equals("")){
                criteria.andIncomeIdEqualTo(shopMerchantIncome.getIncomeId());
            }

            List<ShopMerchantIncome> shopMerchantIncomes = shopMerchantIncomeMapper.selectByExample(shopMerchantIncomeExample);


            return shopMerchantIncomes;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MerchantIncomeException();
        }

    }
}
